package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.entity.Dep;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.DepMapper;
import com.npjscg.medicalpro.service.IDepService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-06-25
 */
@Service
public class DepServiceImpl extends ServiceImpl<DepMapper, Dep> implements IDepService {

    @Resource
    private DepMapper depMapper;

    @Override
    public List<Dep> getDepByfather(int f_id) {

        QueryWrapper<Dep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_id",f_id);
        int one = count(queryWrapper);
        if(one > 0){
            return list(queryWrapper);

        }
        else
            throw new ServiceException(Constants.CODE_600, "该部门没有孩子部门");

    }

    @Override
    public void deleteDep(int id) {
        QueryWrapper<Dep> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Dep> queryWrapperChild = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapperChild.eq("f_id", id);
        Dep one = getOne(queryWrapper);
        int count = count(queryWrapperChild);
        if (one == null ){
            throw new ServiceException(Constants.CODE_600, "没有该部门信息，删除出错");
        }
        else if (count > 0)
            throw new ServiceException(Constants.CODE_600, "存在子部门，请先删除子部门");
        else
            remove(queryWrapper);
    }

    @Override
    public Dep newDep(Dep dep) {
        Dep fDep = getById(dep.getfId());
        if (fDep == null)
            throw  new ServiceException(Constants.CODE_600, "未查询到该父部门");
        else {
            dep.setfName(fDep.getDepname());
            save(dep);
        }
        return null;
    }

    @Override
    public Dep updatename(String newname, String newdepcode, int id) {
        QueryWrapper<Dep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Dep dep = getOne(queryWrapper);
        QueryWrapper<Dep> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("depcode", newdepcode);
        int count = count(queryWrapper2);
        if (dep == null){
            throw new ServiceException(Constants.CODE_400 ,"数据库中没有此部门");
        }
        else if (count > 0) {
            throw new ServiceException(Constants.CODE_400, "部门代码重复");
        } else {
            dep.setDepname(newname);
            dep.setDepcode(newdepcode);
            updateById(dep);
            return dep;
        }
    }

    @Override
    public IPage selectPage(Page<Dep> page, String depname) {
        QueryWrapper<Dep> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("depname",depname);
        return depMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Dep getByCode(String depcode) {
        QueryWrapper<Dep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("depcode", depcode);
        Dep one = getOne(queryWrapper);
        if (one == null)
            throw new ServiceException(Constants.CODE_600,"数据库没有该部门");
        return one;
    }


}
